package task3

import grails.converters.JSON

class OrganizationController {

    def organizationService

    def index() { redirect(action: org1) }

    def org1 = {}

    def jsonDynamic = {
        render organizationService.parseInitialTreeToJson()
    }

    def jsonUpdateNodeName = {
        render organizationService.updateNodeName(params) as JSON
    }


    def jsonDeleteNode = {
        render(organizationService.deleteNode(params) as JSON)
    }

    def saveDataFromForm = {
        def resultMap
        if (params.id == null) {
            resultMap = organizationService.saveNewNode(params)
        } else {
            resultMap = organizationService.updateNode(params)
        }
        render(resultMap as JSON)
    }

    def loadDataToForm = {
        render(organizationService.loadNodeData(params) as JSON)
    }

}